package io.finer.erp.jeecg.bas.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.finer.erp.jeecg.bas.dto.BasCustomerAddressDTO;
import io.finer.erp.jeecg.bas.entity.BasCustomerAddress;
import io.finer.erp.jeecg.bas.service.IBasCustomerAddressService;
import io.finer.erp.jeecg.stock.util.ScanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

 /**
 * @Description: 客户地址表
 * @Author: jeecg-boot
 * @Date:   2020-10-09
 * @Version: V1.0
 */
@Api(tags="客户地址表")
@RestController
@RequestMapping("/bas/basCustomerAddress")
@Slf4j
public class BasCustomerAddressController extends JeecgController<BasCustomerAddress, IBasCustomerAddressService> {
	@Autowired
	private IBasCustomerAddressService basCustomerAddressService;
	
	/**
	 * 分页列表查询
	 *
	 * @param basCustomerAddress
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "客户地址表-分页列表查询")
	@ApiOperation(value="客户地址表-分页列表查询", notes="客户地址表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BasCustomerAddress basCustomerAddress,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BasCustomerAddress> queryWrapper = QueryGenerator.initQueryWrapper(basCustomerAddress, req.getParameterMap());
		Page<BasCustomerAddress> page = new Page<BasCustomerAddress>(pageNo, pageSize);
		List<BasCustomerAddress> list= Lists.newArrayList();
		if(basCustomerAddress!=null && StringUtils.isNotBlank(basCustomerAddress.getCustomerId())) {
			list = basCustomerAddressService.getAddressByCustomerId(basCustomerAddress.getCustomerId());
		}
		IPage<BasCustomerAddress> pageList = basCustomerAddressService.page(page, queryWrapper);
		if(list!=null &&list.size()>0 ){
			pageList.setRecords(list);
		}
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param basCustomerAddress
	 * @return
	 */
	@AutoLog(value = "客户地址表-添加")
	@ApiOperation(value="客户地址表-添加", notes="客户地址表-添加")
	@PostMapping(value = "/add")
	@CacheEvict(value=CacheConstant.BAS_CUSTOMER_ADDRESS, allEntries=true)
	public Result<?> add(@RequestBody BasCustomerAddress basCustomerAddress) {
		basCustomerAddressService.save(basCustomerAddress);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param basCustomerAddress
	 * @return
	 */
	@AutoLog(value = "客户地址表-编辑")
	@ApiOperation(value="客户地址表-编辑", notes="客户地址表-编辑")
	@PutMapping(value = "/edit")
	@CacheEvict(value=CacheConstant.BAS_CUSTOMER_ADDRESS, allEntries=true)
	public Result<?> edit(@RequestBody BasCustomerAddress basCustomerAddress) {
		basCustomerAddressService.updateById(basCustomerAddress);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "客户地址表-通过id删除")
	@ApiOperation(value="客户地址表-通过id删除", notes="客户地址表-通过id删除")
	@DeleteMapping(value = "/delete")
	@CacheEvict(value=CacheConstant.BAS_CUSTOMER_ADDRESS, allEntries=true)
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		basCustomerAddressService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "客户地址表-批量删除")
	@ApiOperation(value="客户地址表-批量删除", notes="客户地址表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@CacheEvict(value=CacheConstant.BAS_CUSTOMER_ADDRESS, allEntries=true)
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.basCustomerAddressService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "客户地址表-通过id查询")
	@ApiOperation(value="客户地址表-通过id查询", notes="客户地址表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BasCustomerAddress basCustomerAddress = basCustomerAddressService.getById(id);
		if(basCustomerAddress==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(basCustomerAddress);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param basCustomerAddress
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasCustomerAddress basCustomerAddress) {
        return super.exportXls(request, basCustomerAddress, BasCustomerAddress.class, "客户地址表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasCustomerAddress.class);
    }
    
    /**
     * @Description 根据客户ID查询地址信息
     * @param customId
     * @return
     * Create at: 2020-12-09 14:12:48
     * @author: duany
     * Revision:
     *    2020-12-09 14:12:48 - first revision by duany
     *        
     */
    @PostMapping("getCustomAddress")
    public Result<?> getCustomAddress(@RequestBody BasCustomerAddress basCustomerAddress){
    	String customId = basCustomerAddress.getCustomerId();
    	BasCustomerAddress address = this.basCustomerAddressService.getCustomAddress(customId);
    	if(null == address) {
    		return Result.error("无地址信息");
    	}
    	return Result.ok(address);
    }
    
    
    @GetMapping("getCustomAddressAll")
    public Result<?> getCustomAddressAll(){
    	List<BasCustomerAddressDTO> address = this.basCustomerAddressService.getAddressByCustomerAll();
    	address = ScanUtil.changeListToVector(address);
    	return Result.ok(address);
    }
}
